import React from 'react';
import PropTypes from 'prop-types';
import routes from './routes';
import {Button, Form, Row, Col, Card, Spin, Alert, Divider} from 'antd';
import Return from '../common/Return';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import {FORM_BIG_OPTIONS} from '../constants';
import {injectIntl} from './intl';
import FileUpload from './PageAdd/FileUpload';
import styles from './style.css';

export class UploadPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false
    };
    
    componentDidMount() {
        this.ismounted = true;
    }

    componentWillUnmount() {
        this.ismounted = false;
        this.props.onUmount();
    }

    onClickReturnBtn = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        this.setState({
            loading: true,
        });
        this.props.onSubmit().then(success => {
            if(this.ismounted && !success)
                this.setState({loading: false});
        });
    }
    onRemoveFile = data => {
        this.setState({
            hasOperat: true
        });
        this.props.removeFile(data.id);
    }
    onChangeUploadFile = data => {
        this.setState({
            hasOperat: true
        });
        this.props.addFiles(data);
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {pagePermission, attachments} = this.props;
        const submitable = hasPermissions(PERMISSION.add, pagePermission);
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Form>
                        
                        <Card
                            title={this.props.intl.formatMessage({
                                id: 'uploadPanel.card.title',
                                defaultMessage: '批量上传附件'
                            })}>
                            <Row>
                                <Alert
                                    message={this.props.intl.formatMessage({
                                        id: 'uploadPanel.alert.title',
                                        defaultMessage: '说明'
                                    })}
                                    description={<div>
                                        <ul>
                                            <li>{this.props.intl.formatMessage({
                                                id: 'uploadPanel.alert.content1',
                                                defaultMessage: '文件命名：'
                                            })}<font size="4" color="#003a8c">1324620201201001</font>
                                            <font size="4" color="#f5222d">_1</font></li>
                                            <li><span>{this.props.intl.formatMessage({
                                                id: 'uploadPanel.alert.content21',
                                                defaultMessage: '只有一个附件也需要'
                                            })}<font size="4" color="#f5222d"> _1</font>
                                            {this.props.intl.formatMessage({
                                                id: 'uploadPanel.alert.content22',
                                                defaultMessage: '，多个附件以此类推'
                                            })}
                                            <font size="4" color="#f5222d"> _2</font>，<font size="4" color="#f5222d">_3...</font></span>
                                            </li>
                                            <li><span>{this.props.intl.formatMessage({
                                                id: 'uploadPanel.alert.content3',
                                                defaultMessage: '多次导入，对应索赔单文件累加而不删除'
                                            })}</span></li>
                                        </ul>
                                    </div>}
                                    type="success" />
                                <Divider/>
                            </Row>
                            <Row>
                                <Col {...FORM_BIG_OPTIONS.col}>
                                    <FileUpload onChange={this.onChangeUploadFile} onRemove={this.onRemoveFile} fileList={attachments} />
                                </Col>
                            </Row>
                        </Card>
                        <Card type="inner">
                            <Row>
                                {attachments.length > 0 && <Col span={4}>
                                    <span>{this.props.intl.formatMessage({
                                        id: 'uploadPanel.card.span',
                                        defaultMessage: '附件总数：'
                                    })}{attachments.length}</span>
                                </Col>}
                                <Col span={attachments.length > 0 ? 20 : 24} disabled={!submitable} className="col-align-right">
                                    <Button key="submit" loading={this.state.loading}
                                        onClick={this.onSubmit} type="primary">{formatMessage({
                                            id: 'uploadPanel.btn.submit',
                                            defaultMessage: '保存'
                                        })}</Button>
                                </Col>
                            </Row>
                        </Card>
                    </Form>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        onConfirm={this.onClickReturnBtn}
                        disabled={this.state.loading}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

UploadPanel.propTypes = {
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    pagePermission: PropTypes.array,
};
import {connect} from 'react-redux';
import {
    onUpload,
    tableSearch,
    addFiles,
    removeFile,
    clearData
} from './actions';
import {selectorFactory} from 'Shared/utils/immutableToJsSelectorFactory';
const getPagePermission = selectorFactory(['page', 'domainData', 'permissions', 'data']);
const getAttachments = selectorFactory(['page', 'domainData', 'athFile', 'attachments']);

const mapStateToProps = state => ({
    pagePermission: getPagePermission(state),
    attachments: getAttachments(state),
});

const mapDispatchToProps = (dispatch, props) => {
    const ownProps = {
        onSubmit: () => dispatch(onUpload()).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
            return result.success;
        }),
        addFiles: data => dispatch(addFiles(data)),
        removeFile: id => dispatch(removeFile(id)),
        onUmount: () => dispatch(clearData()),
        onCancel: () => props.history.push(routes.query.url())
    };
    return ownProps;
};

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(UploadPanel));
